package com.meeting.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.meeting.dto.OrdersQueryDTO;
import com.meeting.dto.RoomBookDTO;
import com.meeting.entity.Orders;
import com.meeting.mapper.OrdersMapper;
import com.meeting.service.OrdersService;
import com.meeting.utils.UserContext;
import com.meeting.vo.OrdersVO;
import com.meeting.vo.PageResult;
import com.meeting.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 创建订单
     * @param roomBookDTO
     * @return
     */
    @Override
    public Orders createOrders(RoomBookDTO roomBookDTO) {
        Orders orders= new Orders();
        long hours = ChronoUnit.HOURS.between(roomBookDTO.getStartTime(), roomBookDTO.getEndTime());
        orders.setPrice(new BigDecimal(hours*roomBookDTO.getPrice()));
        BeanUtils.copyProperties(roomBookDTO,orders);
        orders.setCustomerId(UserContext.getId());
        orders.setSaleTime(LocalDateTime.now());
        orders.setStatus(0);
        ordersMapper.insert(orders);
        return orders;
    }

    /**
     * 支付订单
     * @param id
     */
    @Override
    public void payOrders(Integer id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setPayTime(LocalDateTime.now());
        orders.setStatus(1);
        ordersMapper.updateById(orders);
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void cancelOrders(Integer id) {
        Orders orders = ordersMapper.selectById(id);
        orders.setStatus(4);
        orders.setCancelTime(LocalDateTime.now());
        ordersMapper.updateById(orders);
    }

    /**
     * 分页条件查询所有订单信息
     * @param ordersQueryDTO
     * @return
     */
    @Override
    public PageResult<OrdersVO> listAll(OrdersQueryDTO ordersQueryDTO) {
        ordersQueryDTO.setCustomerId(UserContext.getId());
        PageHelper.startPage(ordersQueryDTO.getPage(),ordersQueryDTO.getPageSize());
        Page<OrdersVO> orders = ordersMapper.listAll(ordersQueryDTO);
        return new PageResult<>(orders.getTotal(),orders.getResult());
    }

    /**
     * 查看退款审核订单
     * @param ordersQueryDTO
     * @return
     */
    public PageResult<OrdersVO> listAllRefundOrders(OrdersQueryDTO ordersQueryDTO) {
        PageHelper.startPage(ordersQueryDTO.getPage(),ordersQueryDTO.getPageSize());
        Page<OrdersVO> orders = ordersMapper.listAll(ordersQueryDTO);
        return new PageResult<>(orders.getTotal(),orders.getResult());
    }

    /**
     * 订单发起退款
     * @param id
     * @return
     */
    @Override
    public Result refund(Integer id) {
        Orders orders = ordersMapper.selectById(id);
        LocalDateTime refundTime = LocalDateTime.now();
        LocalDateTime startTime = orders.getStartTime();
        BigDecimal price = orders.getPrice();

        Duration duration = Duration.between(refundTime,startTime);
        Long hours = duration.toHours();

        BigDecimal refundPrice;
        if(hours >= 72){
            refundPrice = price;
        }else if(hours >= 48){
            refundPrice = price.multiply(new BigDecimal("0.75"));
        }else if(hours >= 24){
            refundPrice = price.multiply(new BigDecimal("0.25"));
        }else {
            refundPrice = BigDecimal.valueOf(0);
            return Result.error("距离预定的会议开始不足24小时，不能退款");
        }

        orders.setRefundPrice(refundPrice);
        orders.setStatus(6);
        ordersMapper.updateById(orders);
        return Result.success();
    }

    @Override
    public void changeOrders(Integer id) {
        Orders orders = ordersMapper.selectById(id);
        orders.setRefundTime(LocalDateTime.now());
        orders.setStatus(5);
        ordersMapper.updateById(orders);
    }
}